package com.ruoyi.job.service.impl;

import com.ruoyi.job.domain.DataCustGradeInfoNew;
import com.ruoyi.job.domain.DataPlanTaskRelation;
import com.ruoyi.job.domain.DataUserTaskRelation;
import com.ruoyi.job.mapper.InsertTaskInfoMapper;
import com.ruoyi.job.service.InsertTaskInfoService;
import com.ruoyi.job.util.GetOnlyId;
import com.ruoyi.job.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class InsertTaskInfoImpl implements InsertTaskInfoService {
    @Autowired
    private InsertTaskInfoMapper insertTaskInfoMapper;

    @Transactional
    public void relationTaskCustomer(){
        DataPlanTaskRelation dataPlanTaskRelation = new DataPlanTaskRelation();
        dataPlanTaskRelation.setStatus("2");
        List<DataPlanTaskRelation> planTaskVOList = insertTaskInfoMapper
                .selectDataPlanTaskRelationList(dataPlanTaskRelation);

        for (DataPlanTaskRelation temp : planTaskVOList) {

            DataCustGradeInfoNew dataCustGradeInfoNew = new DataCustGradeInfoNew();
            dataCustGradeInfoNew.setPlanCode(temp.getPlanCode());

            List<DataCustGradeInfoNew> dataCustGradeInfo = insertTaskInfoMapper.queryConditionNEW(dataCustGradeInfoNew);
            List<DataCustGradeInfoNew> gradeInfoList  = new ArrayList<DataCustGradeInfoNew>();

            DataCustGradeInfoNew DataCustGradeInfoNew = null;
            if(!ListUtil.isNullOrEmpty(dataCustGradeInfo)) {
                DataCustGradeInfoNew = dataCustGradeInfo.get(0);
            }
            if (DataCustGradeInfoNew != null) {
                gradeInfoList = insertTaskInfoMapper.queryResult(DataCustGradeInfoNew);
            }
            for (DataCustGradeInfoNew gradeInfo : gradeInfoList) {

                DataUserTaskRelation dataUserTaskRelation = new DataUserTaskRelation();
                dataUserTaskRelation.setId(GetOnlyId.getOnlyId());
                dataUserTaskRelation.setIdCard(gradeInfo.getIdCard());
                dataUserTaskRelation.setCusName(gradeInfo.getName());
                dataUserTaskRelation.setTaskId(temp.getTaskId());
                dataUserTaskRelation.setType(gradeInfo.getType());
                dataUserTaskRelation.setMarketId(gradeInfo.getMarketId());

                if ((gradeInfo.getOrg_id_cl() == null || "".equals(gradeInfo.getOrg_id_cl()))
                        && "2".equals(gradeInfo.getType())) {
                    List<Map<String, Object>> list = insertTaskInfoMapper.findOrganizationFromUntechOrgMap();// flag
                    // 0:有地址对照
                    // 1：城区支行
                    if (list != null && list.size() > 0) {
                        String address = insertTaskInfoMapper.getAddress(gradeInfo.getIdCard());
                        if(address != null) {
                            for (Map<String, Object> orgAndAddress : list) {
                                String addr = orgAndAddress.get("ADDRESS").toString();
                                if (addr.contains("支行")) {
                                    addr = addr.substring(0, addr.indexOf("支行"));
                                    // 用身份证地址与UNTECH_ORG_MAP表中比对，如果身份证地址中包含对照表中的地址，则匹配成功，否则按城区支行平均分配
                                    if (address.contains(addr)) {
                                        String orgId = orgAndAddress.get("ORGNO").toString();
                                        gradeInfo.setOrg_id_cl(orgId);
                                        gradeInfo.setUser_id_cl("00000000");
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                dataUserTaskRelation.setUserId(gradeInfo.getUser_id_cl());
                dataUserTaskRelation.setOrgId(gradeInfo.getOrg_id_cl());

                insertTaskInfoMapper.insertDataUserTaskRelation(dataUserTaskRelation);
            }
            //将当前任务改成0 表示处理完成
            temp.setStatus("0");
            insertTaskInfoMapper.updateDataPlanTaskRelation(temp);
        }
    }
}
